<!--
  商家销量统计的横向柱状图
-->

<template>
  <div class="com-container">
    <div class="com-chart" ref="seller_ref"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInstance: null,
      allData: null,
      currentPage: 1, // 当前显示的页数
      totalPages: 0, // 一共有多少页
      timerId: null,
    };
  },
  // created() {
  //   this.$socket.registerCallBack("sellerData", this.getData); 
  // },
  mounted() {
    this.initChart();
    // this.$socket.send({
    //   action: "getData",
    //   socketType: "sellerData",
    //   chartName: "seller",
    //   value: "",
    // })
    this.getData();
    window.addEventListener("resize", this.screenAdpter);
    // 在界面加载完成后，主动进行屏幕的适配
    this.screenAdpter();
  },
  distoryed() {
    clearInterval(this.timerId);
    // 在组件销毁时，将监听器取消掉
    window.removeEventListener("resize", this.screenAdpter);
    // this.$socket.unRegisterCallBack("sellerData"); 
  },
  methods: {
    // 初始化echartInstance对象
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.seller_ref, "chalk");

      const initOption = {
        title: {
          text: "▍商家销售统计",
          textStyle: {
            fontSize: 66,
          },
          top: 20,
          left: 20,
        },
        grid: {
          top: "20%",
          left: "3%",
          bottom: "3%",
          right: "6%",
          containLabel: true,
        },
        xAxis: {
          type: "value",
        },
        yAxis: {
          type: "category",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "line",
            z: 0,
            lineStyle: {
              width: 66,
              color: "#2D3443",
            },
          },
        },
        series: [
          {
            type: "bar",
            barWidth: 66,
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "white",
              },
            },
            itemStyle: {
              barBorderRadius: [0, 33, 33, 0],
              // 指明颜色渐变方向
              // 指明不同颜色百分比之下颜色的值
              color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                {
                  offset: 0,
                  color: "#00E2FF",
                },
                {
                  offset: 1,
                  color: "#0063BF",
                },
              ]),
            },
          },
        ],
      };

      this.chartInstance.setOption(initOption);
      this.chartInstance.on("mouseover", () => {
        clearInterval(this.timerId);
      });
      this.chartInstance.on("mouseout", () => {
        this.startInterval();
      });
    },
    // 获取服务器的数据
    async getData() {
      // http://127.0.0.1:8000/api/seller
      const { data: ret } = await this.$http.get("seller");
      this.allData = ret;
      // 对数据排序
      this.allData.sort((a, b) => {
        return a.value - b.value;
      });
      // 每5个元素显示一页
      this.totalPages =
        this.allData.length % 5 === 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;
      this.updataChart();
      this.startInterval();
    },
    updataChart() {
      const start = (this.currentPage - 1) * 5;
      const end = this.currentPage * 5;
      const showData = this.allData.slice(start, end);
      const sellerName = showData.map((item) => {
        return item.name;
      });
      const sellerValue = showData.map((item) => {
        return item.value;
      });
      const dataOption = {
        yAxis: {
          data: sellerName,
        },
        series: [
          {
            data: sellerValue,
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    startInterval() {
      if (this.timerId) {
        clearInterval(this.timerId);
      }
      this.timerId = setInterval(() => {
        this.currentPage++;
        if (this.currentPage > this.totalPages) {
          this.currentPage = 1;
        }
        this.updataChart();
      }, 3000);
    },
    // 当浏览器的大小发生变化时调用的方法，来完成屏幕的适配
    screenAdpter() {
      console.log(this.$refs.seller_ref.offsetWidth);
      const titleFontSize = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        tooltip: {
          axisPointer: {
            lineStyle: {
              width: titleFontSize,
            },
          },
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [0, titleFontSize / 2, titleFontSize / 2, 0],
            },
          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      // 手动调用图标对象的resize方法，才能产生效果
      this.chartInstance.resize();
    },
  },
};
</script>

<style lang="less" scoped></style>
